// pages/news/index.js
var WxParse = require('../../vendor/wxParse/wxParse.js')

Page({
    data: {
        news: '',
        comments: [],
        newsId: 0,
        blogApp: '',
        commentInput: '',
        commentid: 0,
        pageIndex: 1,
        pageSize: 10,
        user: [],
        addStatus: true,
    },
    onLoad: function (options) {
        wx.showLoading({
            title: '加载中',
        })

        var that = this
        that.setData({
            user: getApp().user
        })

        that.loadNews(options);
    },

    onReachBottom: function () {
        //上拉加载分页评论数据
        this.loadComments(this.data.pageIndex);
    },

    loadNews: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        var that = this;
        wx.request({
            url: 'https://api.cnblogs.com/api/newsitems/' + options.newsId + '/body',
            method: 'GET',
            header: {
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": "Bearer " + getApp().accessToken
            },
            success: function (res) {
                if (res.data !== '') {
                    that.setData({
                        news: res.data,
                        newsId: options.newsId,
                        blogApp: options.blogApp
                    })

                    WxParse.wxParse('news', 'html', that.data.news, that)

                    //加载评论
                    that.loadComments(that.data.pageIndex);
                }


            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },

    loadComments: function (pageIndex) {
        wx.showLoading({
            title: '加载中',
        })

        var that = this
        wx.request({
            url: 'https://api.cnblogs.com/api/news/' + that.data.newsId +
                    '/comments?pageIndex=' + pageIndex + '&pageSize=' + that.data.pageSize,
            method: 'GET',
            header: {
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": "Bearer " + getApp().accessToken
            },
            success: function (res) {
                if (res.data.length > 0) {
                    pageIndex++
                    that.setData({
                        comments: that.data.comments.concat(res.data),
                        pageIndex: pageIndex
                    })
                }
            },
            fail: function () {
                // fail
            },
            complete: function () {
                wx.hideLoading()
            }
        })
    },

    //发布评论
    addComment: function () {
        wx.request({
            url: 'https://api.cnblogs.com/api/news/' + this.data.newsId + '/comments',
            method: 'POST',
            data: {
                "ParentId": 1,
                "content": this.data.commentInput
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                'Authorization': 'Bearer ' + getApp().accessToken,
            },
            success: function (res) {
                if (res.statusCode === 201) {
                    wx.showToast({title: "添加评论成功"})
                } else if (res.statusCode === 500) {
                    wx.showToast({title: res.data.Message})
                }
            }

        });
    },

    //修改评论
    editComment: function () {
        var that = this
        wx.request({
            url: 'https://api.cnblogs.com/api/newscomments/' + that.data.commentid,
            method: 'PATCH',
            data: {
                "": this.data.commentInput
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                'Authorization': 'Bearer ' + getApp().accessToken,
            },
            success: function (res) {
                console.log(res)
                if (res.statusCode === 200) {
                    wx.showToast({title: "修改评论成功"})
                    that.setData({
                        commentInput: event.target.dataset.content,
                        addStatus: true
                    })
                }
            }
        });
    },

    //删除评论
    deleteComment: function (event) {
        var that = this
        var commentId = event.target.dataset.commentid;
        var commentsIndex = event.target.dataset.index;
        wx.request({
            url: 'https://api.cnblogs.com/api/newscomments/' + commentId,
            method: 'DELETE',
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                'Authorization': 'Bearer ' + getApp().accessToken,
            },
            success: function (res) {
                if (res.statusCode === 200) {
                    wx.showToast({title: "删除成功"})
                } else if (res.statusCode === 500) {
                    wx.showToast({title: res.data.ExceptionMessage})
                }
            }
        });
    },

    editCommentStatus: function (event) {
        this.setData({
            commentid: event.target.dataset.commentid,
            commentInput: event.target.dataset.content,
            addStatus: false
        })
    },
    
    cancelComment: function () {
        this.setData({
            commentid: 0,
            commentInput: '',
            addStatus: true
        })
    },
    
    listenCommentInput: function (event) {
        this.data.commentInput = event.detail.value;
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    }
})